﻿#include "VAOneXMLAdaptorMaterialFiber.h"
#include "rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITKAcousticsPhysics/FITKAcousticsMaterialsFiber.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include <QFile>
#include <iostream>



namespace IO
{
    bool VAOneXMLAdaptorMaterialFiber::adaptR()
    {
        // 返回操作是否成功
        return readMaterialFiber();
    }

    bool VAOneXMLAdaptorMaterialFiber::adaptW()
    {
        // 返回操作是否成功
        return writeMaterialFiber();
    }

    bool VAOneXMLAdaptorMaterialFiber::readMaterialFiber()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        auto nameNode = _node->first_node("Name")->first_node();

        if (nameNode == nullptr)
        {
            //材料数据中 名称为必要项 不可为空 并抛出异常日志
            consoleMessage(2, "MaterialFiber name is null");
            return false;
        }

        Acoustics::FITKAcousticsMaterialsFiber* fiber = dynamic_cast<Acoustics::FITKAcousticsMaterialsFiber*>(_dataObj);

        if(!fiber)
        {
            return false;
        }

        //数据读取状态 默认为true
        bool b = true;
        // 名称字符串
        QString strName;
        strName = readStrNodeValue(nameNode, &b);

        if(b)
        {
            fiber->setDataObjectName(strName);
        }

        // 读取各项属性
        // 每个属性可能存在缺省配置
        double dValue = readDoubleAttribute(_node, "density", &b);

        if(b)
        {
            fiber->setDensity(dValue);
        }

        double frValue = readDoubleAttribute(_node, "flow_resistivity", &b);

        if(b)
        {
            fiber->setFlowResistivity(frValue);
        }

        double lfValue = readDoubleAttribute(_node, "loss_factor", &b);

        if(b)
        {
            fiber->setLossFactor(lfValue);
        }

        double pValue = readDoubleAttribute(_node, "porosity", &b);

        if(b)
        {
            fiber->setPorosity(pValue);
        }

        double tlValue = readDoubleAttribute(_node, "thermal_length", &b);

        if(b)
        {
            fiber->setThermalLength(tlValue);
        }

        double tValue = readDoubleAttribute(_node, "tortuosity", &b);

        if(b)
        {
            fiber->setTortuosity(tValue);
        }

        double vlValue = readDoubleAttribute(_node, "viscous_length", &b);

        if(b)
        {
            fiber->setViscousLength(vlValue);
        }
        // 读取备注
        auto note_node = _node->first_node("Note");
        if(note_node)
        {
            auto note = note_node->first_node();

            if (note != nullptr)
            {
                QString strNote = readStrNodeValue(note, &b);

                if(b)
                {
                    fiber->setMaterialDescribe(strNote);
                }
            }
        }
        //读取成功
        return true;
    }

    bool VAOneXMLAdaptorMaterialFiber::writeMaterialFiber()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        Acoustics::FITKAcousticsMaterialsFiber* fiber = dynamic_cast<Acoustics::FITKAcousticsMaterialsFiber*>(_dataObj);

        // 判断指针
        if (!fiber)
        {
            return false;
        }

        writeAttribute(_node, "density", fiber->getDensity());
        writeAttribute(_node, "flow_resistivity", fiber->getFlowResistivity());
        writeAttribute(_node, "porosity", fiber->getPorosity());
        writeAttribute(_node, "tortuosity", fiber->getTortuosity());
        writeAttribute(_node, "thermal_length", fiber->getThermalLength());
        writeAttribute(_node, "viscous_length", fiber->getViscousLength());
        writeAttribute(_node, "loss_factor", fiber->getLossFactor());
        // 创建名称节点
        createNodeElement(_node, "Name", fiber->getDataObjectName());
        // 创建备注节点
        createNodeElement(_node, "Note", fiber->getMaterialDescribe());
        return true;
    }
}
